package com.hc.java.proxy;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/6/8 20:58
 */
public class StaticProxyDemo {
    public static void main(String[] args) {
        // 使用代理对象显示图片
        Image image = new ImageProxy("test.jpg");
        image.display();
        image.display();
    }
}

interface Image {
    void display();
}

class RealImage implements Image {
    private String filename;

    public RealImage(String filename) {
        this.filename = filename;
        loadFromDisk();
    }

    private void loadFromDisk() {
        System.out.println("Loading image:" + filename);
    }

    @Override
    public void display() {
        System.out.println("Displaying image: " + filename);
    }
}

class ImageProxy implements Image {

    private String filename;
    private RealImage realImage;

    public ImageProxy(String filename) {
        this.filename = filename;
    }

    @Override
    public void display() {
        System.out.println("代理图片");
        if (realImage == null) {
            realImage = new RealImage(filename);
        }
        realImage.display();
    }
}